package com.next

import static org.springframework.http.HttpStatus.*
import grails.gorm.transactions.Transactional
import org.springframework.security.access.annotation.Secured

@Secured(['ROLE_ADMINISTRATOR', 'ROLE_CUSTOMER_SERVICE_REPRESENTATIVE', 'ROLE_ACCOUNT_MANAGER', 'ROLE_COMPANY_ADMINISTRATOR', 'ROLE_GENERAL_RISK_MANAGER', 'ROLE_BRANCH_GENERAL_MANAGER', 'ROLE_BRANCH_OFFICE_RISK_MANAGER', 'ROLE_HEAD_OFFICE_RISK_MANAGER', 'ROLE_GENERAL_MANAGER', 'ROLE_CRO', 'ROLE_CEO', 'ROLE_POST_LOAN_MANAGER', 'ROLE_BRANCH_OFFICE_POST_LOAN_MANAGER'])
@Transactional(readOnly = true)
class AccountListedInfoController
{

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index(Integer max)
    {
        params.max = Math.min(max ?: 10, 100)
        respond AccountListedInfo.list(params), model: [accountListedInfoCount: AccountListedInfo.count()]
    }

    def show(AccountListedInfo accountListedInfo)
    {
        respond accountListedInfo
    }

    def create()
    {
        def accountId = params.account
        def targetUri = request.getHeader("referer")
        respond new AccountListedInfo(params), model: [targetUri: targetUri, accountId: accountId]
    }

    @Transactional
    def save(AccountListedInfo accountListedInfo)
    {
        if (accountListedInfo == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }
        if (accountListedInfo.hasErrors())
        {
            transactionStatus.setRollbackOnly()
            respond accountListedInfo.errors, model: [targetUri: params['targetUri'], accountId: accountListedInfo?.account?.id], view: 'create'
            return
        }
        accountListedInfo.save flush: true
        redirect controller: "account", action: "show1", method: "GET", id: accountListedInfo?.account?.id
    }

    def edit(AccountListedInfo accountListedInfo)
    {
        def targetUri = request.getHeader("referer")
        respond accountListedInfo, model: [targetUri: targetUri]
    }

    @Transactional
    def update(AccountListedInfo accountListedInfo)
    {
        if (accountListedInfo == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (accountListedInfo.hasErrors())
        {
            transactionStatus.setRollbackOnly()
            respond accountListedInfo.errors, model: [targetUri: params['targetUri']], view: 'edit'
            return
        }

        accountListedInfo.save flush: true
        redirect controller: "account", action: "show1", method: "GET", id: accountListedInfo?.account?.id

    }

    @Transactional
    def delete(AccountListedInfo accountListedInfo)
    {

        if (accountListedInfo == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        accountListedInfo.delete flush: true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'accountListedInfo.label', default: 'AccountListedInfo'), accountListedInfo.id])
                redirect action: "index", method: "GET"
            }
            '*' { render status: NO_CONTENT }
        }
    }

    protected void notFound()
    {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'accountListedInfo.label', default: 'AccountListedInfo'), params.id])
                redirect action: "index", method: "GET"
            }
            '*' { render status: NOT_FOUND }
        }
    }
}
